<?php

namespace Plugin\MultiSeller\Http\Controllers\Shop\Seller;

use Beike\Libraries\Breadcrumb;
use Beike\Repositories\CategoryRepo;
use Beike\Seller\Repositories\ProductRepo;
use Beike\Seller\Repositories\SellerCategoryRepo;
use Beike\Shop\Http\Controllers\Controller;
use Beike\Shop\Http\Resources\ProductSimple;
use Illuminate\Http\Request;

class CategoryController extends Controller
{
    public function show(Request $request, int $sid, int $id)
    {
        $category = SellerCategoryRepo::find($id);
        if (! $category->active) {
            return redirect(shop_route('seller.home.index', ['sid' => $sid]));
        }
        $filterData = $request->only('attr', 'price', 'sort', 'order', 'per_page');
        $products   = ProductRepo::getProductsBySellerCategory($category->id, $filterData);
        $category->load('description');

        $data       = [
            'all_categories'  => SellerCategoryRepo::list($sid),
            'category'        => $category,
            'breadcrumb'      => Breadcrumb::getInstance()->addLink(shop_route('seller_categories.show', ['sid' => $sid, 'id' => $category->id]), $category->description->name),
            'products_format' => ProductSimple::collection($products)->jsonSerialize(),
            'products'        => $products,
            'per_pages'       => CategoryRepo::getPerPages(),
        ];

        return view('MultiSeller::shop.seller.category', $data);
    }
}
